package com.lyl.aop.aspect;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Aspect
@Component
public class LogAspect {
    @Resource
    private HttpServletRequest request;

    @Pointcut("execution( * com.lyl.aop.controller.*.*(..))")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object aroundLog(ProceedingJoinPoint pjp) throws Throwable {
        String queryString = request.getQueryString();
        String url = request.getRequestURL() + (StrUtil.isBlank(queryString) ? "" : "?" + queryString);
        log.info("请求链接: {}", url);
        Object obj = pjp.proceed();
        log.info("请求结果: " + obj);
        return obj;
    }
}
